package honest.playmusic.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 文件管理
 * @author 李跃东
 * @mail androiddevelop@qq.com
 * @date 2013-11-8
 */
public class FileManager{
	/**
	 * 读取文件
	 * @param path 文件路径
	 * @return 文件内容
	 * @throws IOException 
	 */
	public static String getFileContent(String path) throws IOException{
		StringBuffer res=new StringBuffer("");
		File file=new File(path);
		if(!file.exists()){
			throw new FileNotFoundException();
		}
		BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
		String tmp;
		while((tmp=bufferedReader.readLine())!=null)
			res.append(tmp);
		bufferedReader.close();
		return  res.toString();
	}

	/**
	 *  写入文件
	 * @param path 文件路径
	 * @param content 文件内容
	 * @return
	 * @throws IOException 
	 */
	public boolean writeFile(String path,String content) throws IOException{
		if(content==null||content.length()==0)
			return false;
		File file=new File(path); 
		BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(file));
		bufferedWriter.write(content);
		bufferedWriter.flush();
		bufferedWriter.close();
		return true;
	}
}
